package org.dromara.mqtt.tools;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;
import java.util.HashMap;

@Slf4j
public class MqttMessageBuilder {

    private final ObjectMapper objectMapper = new ObjectMapper();

    /**
     * 构建设备控制指令消息
     */
    public String buildControlMessage(String method, String clientToken, Map<String, Object> params) {
        try {
            ObjectNode message = objectMapper.createObjectNode();
            message.put("method", method);
            message.put("clientToken", clientToken);

            // 添加params对象
            if (params != null) {
                ObjectNode paramsNode = message.putObject("params");
                for (Map.Entry<String, Object> entry : params.entrySet()) {
                    if (entry.getValue() instanceof Map) {
                        // 处理嵌套对象，如定时器配置
                        ObjectNode nestedNode = paramsNode.putObject(entry.getKey());
                        Map<String, Object> nestedMap = (Map<String, Object>) entry.getValue();
                        for (Map.Entry<String, Object> nestedEntry : nestedMap.entrySet()) {
                            if (nestedEntry.getValue() instanceof Integer) {
                                nestedNode.put(nestedEntry.getKey(), (Integer) nestedEntry.getValue());
                            } else if (nestedEntry.getValue() instanceof String) {
                                nestedNode.put(nestedEntry.getKey(), (String) nestedEntry.getValue());
                            }
                        }
                    } else if (entry.getValue() instanceof Integer) {
                        paramsNode.put(entry.getKey(), (Integer) entry.getValue());
                    } else if (entry.getValue() instanceof String) {
                        paramsNode.put(entry.getKey(), (String) entry.getValue());
                    }
                }
            }

            return objectMapper.writeValueAsString(message);
        } catch (Exception e) {
           log.error("构建设备控制指令消息失败", e);
            return null;
        }
    }

    /**
     * 构建电源控制消息示例
     */
    public String buildPowerControlMessage(String clientToken, int powerSwitch) {
        Map<String, Object> params = new HashMap<>();
        params.put("power_switch", powerSwitch);
        return buildControlMessage("set", clientToken, params);
    }

    /**
     * 构建档位控制消息示例
     */
    public String buildGearControlMessage(String clientToken, String gear) {
        Map<String, Object> params = new HashMap<>();
        params.put("gear", gear);
        return buildControlMessage("set", clientToken, params);
    }

    /**
     * 构建温度控制消息示例
     */
    public String buildTemperatureControlMessage(String clientToken, int temperature) {
        Map<String, Object> params = new HashMap<>();
        params.put("temperature", temperature);
        return buildControlMessage("set", clientToken, params);
    }

    /**
     * 构建定时器控制消息示例
     */
    public String buildTimerControlMessage(String clientToken, Map<String,Object> timers) {
        return buildControlMessage("set", clientToken, timers);
    }
}

